package com.rds.common.utils;

import com.rds.common.annotation.SysElasticSearchMapper2;
import com.rds.common.core.controller.BaseController;
import com.rds.common.core.domain.entity.SysElasticSearch;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;


public class EsUtils  extends BaseController {



    /**
     * 将数据保存到es中
     * @param sysElasticSearchMapper
     * @param id 操作id
     * @param typeStr 操作类型
     * @param details 操作细节
     * @param module 模块名称
     * @return
     */
    public static int addHistory(SysElasticSearchMapper2 sysElasticSearchMapper, Long id, String typeStr, String details, String module) {
        System.out.println("开始保存数据到ES...测试用");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        long millis = calendar.getTimeInMillis();
        Date currentDate = new Date();
        currentDate.setTime(millis);
        Date d = new Date();
        try{
            d = sdf.parse(sdf.format(currentDate));
        }catch(Exception ex){
            ex.getMessage();
        }
        SysElasticSearch elasticSearch = new SysElasticSearch();
        elasticSearch.setOperateId(new Random().nextInt(10000)+1);
        //elasticSearch.setOperateId(new Random().nextInt(10)+1);
        elasticSearch.setUsername(SecurityUtils.getUsername());
        elasticSearch.setModule(module);
        //elasticSearch.setDocId(Math.toIntExact(id));
        elasticSearch.setDocId(id);
        elasticSearch.setOperation(typeStr);
        elasticSearch.setOperationDetail(details);
        elasticSearch.setModified(d);
        System.out.println("开始保存数据到ES...测试用3333333333333333333333");
        System.out.println("=========="+elasticSearch);
        //sysElasticSearchMapper.save(elasticSearch);
        System.out.println("数据保存成功...");
        return 0;
    }
}
